package com.cooper.scraper;

import android.util.Log;

import com.cooper.android.comm.Resource;

public class BrowseResourceDescCollector <T extends Resource> extends ResourceCollector<T>{
	
	private static final String BROWSE_DESC_LIST_KEY = "BROWSE_DESC_LIST_";
	
	public BrowseResourceDescCollector(int pageCount){
		super(pageCount);
		this.pageCount = pageCount;
	}
	
	@Override
	public boolean EOF() {
		return (1 < pageNumber);
	}

	@Override
	public boolean hasNextPage() {
		return (pageNumber > 1);
	}
	
	@Override
	public void nextPage() {
		pageNumber--;
	}
	
	@Override
	public void reset() {
		medias = null;
		pageNumber = pageCount;
	}

	@Override
	public String generateKey() {
		return BROWSE_DESC_LIST_KEY  + getPageNumber();
	}

	@Override
	public ResultUrl scraperListUrl(ResourceScraper<T> scraper)
			throws ScraperException, DownloadException {
		return scraper.createBrowseUrl(getPageNumber());
	}

	@Override
	public void scraperResult(ResourceScraper<T> scraper, String page)
			throws ScraperException, DownloadException {
		medias = scraper.extractBrowseResource(page);
		Log.d("BrowseResultMedias","count:" + medias.size());		
	}

	@Override
	public void scraperResult(ResourceScraper<T> scraper, String page,
			String redirectUrl) throws ScraperException, DownloadException {
		medias = scraper.extractBrowseResource(page,redirectUrl);	
	}

	@Override
	public boolean subscribed(String key) {
		return key.startsWith(BROWSE_DESC_LIST_KEY);
	}

}
